package com.pan.studentmaster;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.Log;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

public class StationaryDialog extends Dialog  implements OnTabChangeListener {

	public StationaryDialog(Activity activity, MenuItem menuItem) {
		super(activity);
		_menuItem = menuItem;
		setTitle("Stationary");
		setContentView(R.layout.layout_stationary);
		Resources r = Main.instance().getResources();
		TabHost th = (TabHost)findViewById(R.id.tabhost1);
		th.setup();
		th.addTab(th.newTabSpec("Pencil").setIndicator(null,
			r.getDrawable(R.drawable.ic_pencil)).setContent(R.id.pencil_list));
		th.addTab(th.newTabSpec("Eraser").setIndicator(null,
			r.getDrawable(R.drawable.ic_eraser)).setContent(R.id.eraser_list));
		th.addTab(th.newTabSpec("Highlighter").setIndicator(null,
			r.getDrawable(R.drawable.ic_highlighter)).setContent(R.id.highlighter_list));
		th.addTab(th.newTabSpec("ColorPencil").setIndicator(null,
			r.getDrawable(R.drawable.ic_colorpencil)).setContent(R.id.colorpencil_list));
		th.setOnTabChangedListener(this);
		_pencils = (ListView)findViewById(R.id.pencil_list);
		_erasers = (ListView)findViewById(R.id.eraser_list);
		_highlighters = (ListView)findViewById(R.id.highlighter_list);
		_colorPencils = (ListView)findViewById(R.id.colorpencil_list);
		// setup all lists
		_pencilsAdapter = new StationaryAdapter(activity, _pencilResources);
		_pencils.setAdapter(_pencilsAdapter);
		_pencils.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		_pencils.setOnItemClickListener(_pencilsAdapter);
		_erasersAdapter = new StationaryAdapter(activity, _eraserResources);
		_erasers.setAdapter(_erasersAdapter);
		_erasers.setOnItemClickListener(_erasersAdapter);
		_highlightersAdapter = new StationaryAdapter(activity, _hlResources);
		_highlighters.setAdapter(_highlightersAdapter);
		_highlighters.setOnItemClickListener(_highlightersAdapter);
		_colorPencilsAdapter = new StationaryAdapter(activity, _cpResources);
		_colorPencils.setAdapter(_colorPencilsAdapter);
		_colorPencils.setOnItemClickListener(_colorPencilsAdapter);
	}

	@Override
	public void onTabChanged(String tabName) {
		int id = 0;
		if(tabName == "Pencil") {
			_pencilsAdapter.notifyDataSetChanged();
			_menuItem.setIcon(R.drawable.ic_pencil);
			if(_pencilsAdapter._selected >= 0)
				id = _pencilResources[_pencilsAdapter._selected];
			else
				id = R.drawable.ic_pencil_hb; 
		}
		else if(tabName == "Eraser") {
			_erasersAdapter.notifyDataSetChanged();
			_menuItem.setIcon(R.drawable.ic_eraser);
			if(_erasersAdapter._selected >= 0)
				id = _eraserResources[_erasersAdapter._selected];
			else
				id = R.drawable.ic_eraser_regular;
		}
		else if(tabName == "Highlighter") {
			_highlightersAdapter.notifyDataSetChanged();
			_menuItem.setIcon(R.drawable.ic_highlighter);
			if(_highlightersAdapter._selected >= 0)
				id = _hlResources[_highlightersAdapter._selected];
			else
				id = R.drawable.ic_highlighter_yellow;
		}
		else if(tabName == "ColorPencil") {
			_colorPencilsAdapter.notifyDataSetChanged();
			_menuItem.setIcon(R.drawable.ic_colorpencil);
			if(_colorPencilsAdapter._selected >= 0)
				id = _cpResources[_colorPencilsAdapter._selected];
			else
				id = R.drawable.ic_colorpencil_ff0000;
		}
		int stationary = _resMap.get(id);
		Stationary.setCurrentStationary(stationary);
		Log.i("DLG", "Set stationary " + Integer.toHexString(stationary));
	}

	private MenuItem						_menuItem;
	private ListView						_pencils;
	private StationaryAdapter				_pencilsAdapter;
	private ListView						_erasers;
	private StationaryAdapter				_erasersAdapter;
	private ListView						_highlighters;
	private StationaryAdapter				_highlightersAdapter;
	private ListView						_colorPencils;
	private StationaryAdapter				_colorPencilsAdapter;
	private Integer[]						_pencilResources = new Integer[] {
		R.drawable.ic_pencil_2h,
		R.drawable.ic_pencil_1h,
		R.drawable.ic_pencil_hb,
		R.drawable.ic_pencil_1b,
		R.drawable.ic_pencil_2b
	};
	private Integer[]						_eraserResources = new Integer[] {
		R.drawable.ic_eraser_regular,
		R.drawable.ic_eraser_super,
		R.drawable.ic_eraser_omni
	};
	private Integer[]						_hlResources = new Integer[] {
		R.drawable.ic_highlighter_yellow,
		R.drawable.ic_highlighter_green,
		R.drawable.ic_highlighter_pink
	};
	private Integer[]						_cpResources = new Integer[] {
		R.drawable.ic_colorpencil_0080ff,
		R.drawable.ic_colorpencil_80ff80,
		R.drawable.ic_colorpencil_4040c0,
		R.drawable.ic_colorpencil_408040,
		R.drawable.ic_colorpencil_804040,
		R.drawable.ic_colorpencil_ff0000,
		R.drawable.ic_colorpencil_ff8000,
		R.drawable.ic_colorpencil_ff8080,
		R.drawable.ic_colorpencil_ffc0ff,
		R.drawable.ic_colorpencil_ffc080,
		R.drawable.ic_colorpencil_ffff00
	};

	public class StationaryAdapter extends ArrayAdapter<Integer> implements OnItemClickListener {
		
		private Integer[]					_ids;
		private int							_selected = -1;

		public StationaryAdapter(Context context, Integer[] ids) {
			super(context, R.layout.layout_stationary_item, R.id.textView1, ids);
			_ids = ids;
			setNotifyOnChange(true);
		}
		
		@Override
		public View getView(int position, View v, ViewGroup parent) {
			if(v == null) {
				LayoutInflater inflater =
					(LayoutInflater)Main.instance().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.layout_stationary_item, parent, false);
			}
			TextView textView = (TextView)v.findViewById(R.id.textView1);
			ImageView imageView = (ImageView)v.findViewById(R.id.imageView1);
			imageView.setImageResource(_ids[position]);
			String s = _labelMap.get(_ids[position]);
			textView.setText(s);
			if(_selected == position) {
				v.setBackgroundColor(Color.CYAN);
			}
			else if(_selected < 0 &&
				(_ids[position] == R.drawable.ic_pencil_hb ||
					_ids[position] == R.drawable.ic_eraser_regular ||
					_ids[position] == R.drawable.ic_highlighter_yellow ||
					_ids[position] == R.drawable.ic_colorpencil_ff0000)) {
				_selected = position;
				v.setBackgroundColor(Color.CYAN);
			}
			else {
				v.setBackgroundColor(Color.TRANSPARENT);
			}
			return v;
		}

		@Override
		public void onItemClick(AdapterView<?> parent, View v, int pos, long iid) {
			if(pos != _selected) {
				_selected = pos;
				int stationary = _resMap.get(_ids[pos]);
				Stationary.setCurrentStationary(stationary);
				Log.i("DLG", "Set stationary " + Integer.toHexString(stationary));
				notifyDataSetChanged();
			}
		}
	}
	
	static SparseIntArray		_resMap = new SparseIntArray();
	static {
		_resMap.put(R.drawable.ic_pencil_2h, Stationary.PENCIL * Stationary.M + Stationary.P_2H);
		_resMap.put(R.drawable.ic_pencil_1h, Stationary.PENCIL * Stationary.M + Stationary.P_1H);
		_resMap.put(R.drawable.ic_pencil_hb, Stationary.PENCIL * Stationary.M + Stationary.P_HB);
		_resMap.put(R.drawable.ic_pencil_1b, Stationary.PENCIL * Stationary.M + Stationary.P_1B);
		_resMap.put(R.drawable.ic_pencil_2b, Stationary.PENCIL * Stationary.M + Stationary.P_2B);
		_resMap.put(R.drawable.ic_eraser_regular, Stationary.ERASER * Stationary.M + Stationary.E_REGULAR);
		_resMap.put(R.drawable.ic_eraser_super, Stationary.ERASER * Stationary.M + Stationary.E_SUPER_PENCIL);
		_resMap.put(R.drawable.ic_eraser_omni, Stationary.ERASER * Stationary.M + Stationary.E_SUPER_OMNI);
		_resMap.put(R.drawable.ic_highlighter_green, Stationary.HIGH_LIGHTER * Stationary.M + Stationary.HL_GREEN);
		_resMap.put(R.drawable.ic_highlighter_pink, Stationary.HIGH_LIGHTER * Stationary.M + Stationary.HL_PINK);
		_resMap.put(R.drawable.ic_highlighter_yellow, Stationary.HIGH_LIGHTER * Stationary.M + Stationary.HL_YELLOW);
		_resMap.put(R.drawable.ic_colorpencil_0080ff, Stationary.COLOR_PENCIL * Stationary.M + 0x0080ff);
		_resMap.put(R.drawable.ic_colorpencil_80ff80, Stationary.COLOR_PENCIL * Stationary.M + 0x80ff80);
		_resMap.put(R.drawable.ic_colorpencil_4040c0, Stationary.COLOR_PENCIL * Stationary.M + 0x4040c0);
		_resMap.put(R.drawable.ic_colorpencil_408040, Stationary.COLOR_PENCIL * Stationary.M + 0x408040);
		_resMap.put(R.drawable.ic_colorpencil_804040, Stationary.COLOR_PENCIL * Stationary.M + 0x804040);
		_resMap.put(R.drawable.ic_colorpencil_ff0000, Stationary.COLOR_PENCIL * Stationary.M + 0xff0000);
		_resMap.put(R.drawable.ic_colorpencil_ff8000, Stationary.COLOR_PENCIL * Stationary.M + 0xff8000);
		_resMap.put(R.drawable.ic_colorpencil_ff8080, Stationary.COLOR_PENCIL * Stationary.M + 0xff8080);
		_resMap.put(R.drawable.ic_colorpencil_ffc0ff, Stationary.COLOR_PENCIL * Stationary.M + 0xffc0ff);
		_resMap.put(R.drawable.ic_colorpencil_ffc080, Stationary.COLOR_PENCIL * Stationary.M + 0xffc080);
		_resMap.put(R.drawable.ic_colorpencil_ffff00, Stationary.COLOR_PENCIL * Stationary.M + 0xff0000);
	};

	static SparseArray<String>		_labelMap = new SparseArray<String>();
	static {
		_labelMap.put(R.drawable.ic_pencil_2h, "2H");
		_labelMap.put(R.drawable.ic_pencil_1h, "1H");
		_labelMap.put(R.drawable.ic_pencil_hb, "HB");
		_labelMap.put(R.drawable.ic_pencil_1b, "1B");
		_labelMap.put(R.drawable.ic_pencil_2b, "2B");
		_labelMap.put(R.drawable.ic_eraser_regular, "REGULAR");
		_labelMap.put(R.drawable.ic_eraser_super, "SUPER");
		_labelMap.put(R.drawable.ic_eraser_omni, "OMNI");
		_labelMap.put(R.drawable.ic_highlighter_green, "GREEN");
		_labelMap.put(R.drawable.ic_highlighter_pink, "PINK");
		_labelMap.put(R.drawable.ic_highlighter_yellow, "YELLOW");
		_labelMap.put(R.drawable.ic_colorpencil_0080ff, "0080ff");
		_labelMap.put(R.drawable.ic_colorpencil_80ff80, "80ff80");
		_labelMap.put(R.drawable.ic_colorpencil_4040c0, "4040c0");
		_labelMap.put(R.drawable.ic_colorpencil_408040, "408040");
		_labelMap.put(R.drawable.ic_colorpencil_804040, "804040");
		_labelMap.put(R.drawable.ic_colorpencil_ff0000, "ff0000");
		_labelMap.put(R.drawable.ic_colorpencil_ff8000, "ff8000");
		_labelMap.put(R.drawable.ic_colorpencil_ff8080, "ff8080");
		_labelMap.put(R.drawable.ic_colorpencil_ffc0ff, "ffc0ff");
		_labelMap.put(R.drawable.ic_colorpencil_ffc080, "ffc080");
		_labelMap.put(R.drawable.ic_colorpencil_ffff00, "ff0000");
	};
	
}
